﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;

namespace KegMonitor
{
    public static class KegLog
    {
        private static EventLog _log = new EventLog("KegMonitor");
        private static string _logName = "Application";
        private static string _source = "KegMonitor";
        private static BindingList<string> _logEntries = new BindingList<string>();
        private static int _length = 250;

        public static int MaxLength
        {
            get { return _length;}
            set { _length = value; }
        }

        public static BindingList<string> Entries
        {
            get { return _logEntries; }
        }

        private static void AddEntry(string message)
        {
            while(_logEntries.Count > _length)
            {
                _logEntries.RemoveAt(0);
            }

            _logEntries.Add(message);
        }

        public static void LogError(string errorText)
        {
            //if (!EventLog.SourceExists(_source))
            //{
            //    EventLog.CreateEventSource(_source, _logName);
            //}

            //EventLog.WriteEntry(_source, errorText, EventLogEntryType.Error);
            AddEntry("Error:" + errorText);
        }

        public static void LogWarning(string errorText)
        {
            //if (!EventLog.SourceExists(_source))
            //{
            //    EventLog.CreateEventSource(_source, _logName);
            //}

            //EventLog.WriteEntry(_source, errorText, EventLogEntryType.Warning);
            AddEntry("Warning:" + errorText);
        }

        public static void LogData(string message)
        {
            LogComment(message);
        }

        public static void LogComment(string message)
        {
            Console.WriteLine(message);
            AddEntry(message);

            // TODO: Only keep the last 250 items
        }
    }
}
